<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use App\Models\TiCash;


class TicashDay extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'total:ticashday';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '每日提现统计';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        //
        $today = date('Y-m-d');
        $yesterday = date('Y-m-d', strtotime(" $today -1 day"));
        $sql = DB::select("select type, status, date(created_at) as date, money FROM ticash where date(created_at) = \"$yesterday\"");
        $insert = [
            'apply_bi'=>0,
            'suc_bi'=>0,
            'fail_bi'=>0,
            'apply_money'=>0,
            'suc_money'=>0,
            'fail_money'=>0,
        ];
        foreach ($sql as $k => $v){
            if($v->type == 0){
                $insert['apply_bi'] += (int)$v->money;
                if($v->status == 1){
                    $insert['suc_bi'] += (int)$v->money;
                }else if($v->status == 2){
                    $insert['fail_bi'] += (int)$v->money;
                }
            }else{
                $insert['apply_money'] += $v->money;
                if($v->status == 1){
                    $insert['suc_money'] += $v->money;
                }else if($v->status == 2){
                    $insert['fail_money'] += $v->money;
                }
            }
        }
        $insert['date'] = $yesterday;
        try{
            $res = DB::table('ticash_log')->insert($insert);
        }catch (\Exception $e) {
            //todo 写入日志..

        }
    }
}
